package com.bestcem.xm.ticket.controller;

import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.controller.vo.MemberVO;
import com.bestcem.xm.ticket.grpc.client.TicketMemberGrpcService;
import com.bestcem.xm.ticket.grpc.client.dto.user.MemberDTO;
import com.bestcem.xm.ticket.service.TicketNotificationService;
import com.bestcem.xm.ticket.service.TicketNotifySettingService;
import com.bestcem.xm.ticket.service.TicketService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/20 17:45
 * @deprecated 于联系人P2需求, 转为 member 模块 http 接口, 本处废弃
 */
@Deprecated
@Slf4j
@RestController
@RequestMapping(value = "/ticket", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketController {
    @Resource
    private TicketNotificationService ticketNotificationService;

    @Resource
    private TicketService ticketService;

    @Resource
    private TicketNotifySettingService ticketNotifySettingService;

    @Resource
    private TicketMemberGrpcService ticketMemberGrpcService;

    /**
     * @deprecated 于联系人P2需求, 转为 member 模块 http 接口, 本处废弃
     */
    @Deprecated
    @GetMapping("member/page")
    public XmResultJson<PageDTO<MemberVO>> listOrgMembers(HttpServletRequest request,
                                                          @RequestParam("page") Integer page,
                                                          @RequestParam("rows_per_page") Integer pageSize) {
        PageParam pageParam = new PageParam(page, pageSize);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(pageParam);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            return XmResultJson.fail(0, violation.getMessage());
        }

        // 暂不进行参数校验
        String orgId = SecurityContextHolder.getOrgId();

        // 查询用户信息
        PageDTO<MemberDTO> pageMembers = ticketMemberGrpcService.listMembersByOrgId(orgId, pageParam);
        // 数据转化
        List<MemberVO> memberVos = pageMembers.getData().stream().map(this::convert).collect(Collectors.toList());

        return XmResultJson.success(new PageDTO<>(page, memberVos, pageMembers.getTotal()));
    }

    private MemberVO convert(MemberDTO memberDTO) {
        MemberVO res = new MemberVO();
        if (memberDTO == null) {
            return res;
        }

        res.setId(memberDTO.getId());
        res.setName(memberDTO.getName());
        res.setPhone(memberDTO.getPhone());
        res.setEmail(memberDTO.getMail());
        return res;
    }
}